﻿using aier.dataaccess.model;
using Dapper;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.dataaccess.dapper
{
    public class ScreenItemDal
    {
        public ILog log { get; set; }
        public DapperHelper dapperHelper { get; set; }

        /// <summary>
        /// 获取所有筛查项目
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ScreenItemView> GetScreenAllTeams()
        {
            return dapperHelper.ExecueWithoutTransaction<IEnumerable<ScreenItemView>>(a =>
            {
                string sql = @"select * from AIER_SCREENITEM_INFO ";
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                return a.Query<ScreenItemView>(sqlTemplate.RawSql);
            });
        }

        /// <summary>
        /// 根据医院ID获取筛查项目
        /// </summary>
        /// <param name="hospitalId"></param>
        /// <returns></returns>
        public IEnumerable<ScreenItemView> GetScreenItemsByHospID(Guid hospitalId)
        {

            return dapperHelper.ExecueWithoutTransaction<IEnumerable<ScreenItemView>>(a =>
            {
                string sql = @"select * from AIER_SCREENITEM_INFO /**where**/";
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                sqlBuilder.Where("new_hospitalId = @hospitalId", new { hospitalId = hospitalId });

                return a.Query<ScreenItemView>(sqlTemplate.RawSql, sqlTemplate.Parameters);
            });
        }

        /// <summary>
        /// 根据筛查项目名称查询当前筛查项目
        /// 原程序使用：getScreenteam
        /// </summary>
        /// <param name="name">筛查项目名称</param>
        /// <param name="ownerid">负责人ID</param>
        /// <param name="statecode">状态０：可用,２:停用</param>
        /// <returns></returns>
        public IEnumerable<ScreenItemView> QueryScreenItem(string name, string ownerid, int statecode = 0)
        {
            return dapperHelper.ExecueWithoutTransaction<IEnumerable<ScreenItemView>>(a =>
            {
                string sql = @"select * from new_screenitem  /**where**/  ";
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(sql);

                if (!string.IsNullOrEmpty(ownerid))
                {
                    sqlBuilder.Where("  OwnerId=@OwnerId ", new { OwnerId = ownerid });
                }
                if (!string.IsNullOrEmpty(name))
                {
                    sqlBuilder.Where(" statecode=@statecode ", new { statecode = statecode });
                }
                if (!string.IsNullOrEmpty(name))
                {
                    sqlBuilder.Where("  CHARINDEX(@new_name,new_name)>= 1 ", new { new_name = name });
                }

                return a.Query<ScreenItemView>(sqlTemplate.RawSql, sqlTemplate.Parameters);
            });
        }
    }
}
